<template>
  <div class="login-container">
    <div class="login-box">
      <h2>学生作业提交系统</h2>
      <form id="loginForm" @submit.prevent="handleLogin">
        <div class="form-group">
          <label for="username">用户名</label>
          <input type="text" id="username" v-model="formData.username" required placeholder="请输入用户名">
        </div>
        <div class="form-group">
          <label for="password">密码</label>
          <input type="password" id="password" v-model="formData.password" required placeholder="请输入密码">
        </div>
        <div class="form-group">
          <label for="role">角色</label>
          <select id="role" v-model="formData.role" required>
            <option value="student">学生</option>
            <option value="teacher">教师</option>
          </select>
        </div>
        <button type="submit" class="login-btn">登录</button>
      </form>
    </div>
  </div>
</template>

<script>
import { api } from '@/utils/api.js';
import { showMessage } from '@/utils/common.js';

export default {
  name: 'Login',
  data() {
    return {
      formData: {
        username: '',
        password: '',
        role: 'student'
      }
    };
  },
  methods: {
    /**
     * 处理用户登录
     */
    async handleLogin() {
      try {
        const response = await api.login(this.formData);
        // 适配后端返回的数据格式，从data对象中获取token和user信息
        if (response && response.success && response.data) {
          const { token, user } = response.data;
          // 保存登录信息到本地存储
          localStorage.setItem('token', token);
          localStorage.setItem('userInfo', JSON.stringify(user));
          
          showMessage('登录成功', 'success');
          
          // 根据用户角色跳转到相应页面
          if (user.role === 'teacher') {
            this.$router.push('/teacher/home');
          } else {
            this.$router.push('/student/home');
          }
        }
      } catch (error) {
        // 正确解析后端返回的错误消息格式
        const errorMessage = error.message || error.data?.message || '登录失败，请检查用户名和密码';
        showMessage(errorMessage, 'error');
      }
    }
  },
  mounted() {
    // 检查是否已经登录，如果已登录则直接跳转
    const token = localStorage.getItem('token');
    const userInfo = localStorage.getItem('userInfo');
    if (token && userInfo) {
      const user = JSON.parse(userInfo);
      if (user.role === 'teacher') {
        this.$router.push('/teacher/home');
      } else {
        this.$router.push('/student/home');
      }
    }
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
}

.login-box {
  width: 400px;
  padding: 30px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.login-box h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #666;
  font-weight: 500;
}

.form-group input,
.form-group select {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
  transition: border-color 0.3s;
}

.form-group input:focus,
.form-group select:focus {
  outline: none;
  border-color: #1E9FFF;
}

.login-btn {
  width: 100%;
  padding: 12px;
  background-color: #1E9FFF;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.login-btn:hover {
  background-color: #40A9FF;
}
</style>